package com.qiaofang.housewarehouse.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qiaofang.housewarehouse.dao.EsDAO;
import com.qiaofang.housewarehouse.dao.domain.HswhSaleHouse;
import com.qiaofang.housewarehouse.dao.utils.ConfigUtils;
import com.qiaofang.housewarehouse.service.SaleHouseQueryService;
import com.qiaofang.housewarehouse.service.compose.HswhSaleHouseCompose;
import com.qiaofang.housewarehouse.stub.bean.HswhSaleHouseDetailDTO;
import com.qiaofang.housewarehouse.stub.bean.HswhSaleQueryResponseDTO;
import com.qiaofang.housewarehouse.stub.constants.TableConstants;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.SearchHit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SaleHouseQueryServiceImpl implements SaleHouseQueryService {
    private static final Logger logger = LoggerFactory.getLogger(SaleHouseQueryServiceImpl.class);
    @Resource
    private EsDAO esDAO;

    @Override
    public HswhSaleHouseDetailDTO getSaleHouseDetail(Map<String, String> paramMap,String tableName, String id, Class cls) throws Exception {
        paramMap.put("id", id);
        GetResponse response = esDAO.getIndexById(paramMap);
        if (null != response.getSource()) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", response.getId());
            Map<String, Object> source = response.getSource();
            for (String key : source.keySet()) {
                jsonObject.put(key, source.get(key));
            }
            return HswhSaleHouseCompose.composeDetailDTO(JSONObject.parseObject(jsonObject.toJSONString(), HswhSaleHouse.class));
        } else {
            return null;
        }
    }

    @Override
    public HswhSaleQueryResponseDTO searchSaleHouseList(Map<String, String> paramMap) throws Exception {
        paramMap.put("indexName", TableConstants.INDEX_SALE_HOUSE);
        paramMap.put("typeName", TableConstants.INDEX_TYPE_SALE_HOUSE);
        logger.info("paramMap：{}", paramMap.toString());
        HswhSaleQueryResponseDTO hswhSaleQueryResponseDTO = new HswhSaleQueryResponseDTO();
        SearchResponse searchResponse = esDAO.searchHouseList(paramMap);
        logger.info("查询eslticsearch succee");
        List<HswhSaleHouse> hswhSaleHouses = new ArrayList<>();
        if (searchResponse.getHits().getTotalHits() != 0) {
            for (SearchHit searchHit : searchResponse.getHits().getHits()) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id", searchHit.getId());
                Map<String, Object> source = searchHit.getSource();
                for (String key : source.keySet()) {
                    jsonObject.put(key, source.get(key));
                }
                hswhSaleHouses.add(JSONObject.parseObject(jsonObject.toJSONString(), HswhSaleHouse.class));
            }
            hswhSaleQueryResponseDTO.setHswhSaleHouseListDTOS(HswhSaleHouseCompose.composeListDTO(hswhSaleHouses));
            hswhSaleQueryResponseDTO.setTotalCount(String.valueOf(searchResponse.getHits().getTotalHits()));
        } else {
            hswhSaleQueryResponseDTO.setTotalCount(String.valueOf(0));
        }
        hswhSaleQueryResponseDTO.setPageIndex(paramMap.get("pageIndex"));
        return hswhSaleQueryResponseDTO;
    }
}
